﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Reflection;
using MyBet.Web;
using System.ServiceModel.DomainServices.Client;
using System.ServiceModel.DomainServices.Client.ApplicationServices;

namespace MyBet
{
	public partial class AboutControl : UserControl
	{
        //private UserRegistrationContext userRegistrationContext = new UserRegistrationContext();
        

        private string GetVersion()
        {
            Assembly asm = Assembly.GetExecutingAssembly();
            string[] parts = asm.FullName.Split(',');
            string version = parts[1];
            version = version.Replace("Version=", "");
            version = version.Trim();
            return version;
        }

		public AboutControl()
		{
            DateTime currentDate = DateTime.Now;

			// Required to initialize variables
			InitializeComponent();
            // Version
            VersionLabel.Text = String.Format("Version v{0}", GetVersion());
            // Company
            CompanyLabel.Text = String.Format("Phoenix Copyright (c) {0}", currentDate.Year);
            // Members
            if (App.UserRegistration != null)
            {
                App.UserRegistration.GetNumberOfUsers(this.GetNumberOfUsers_Completed, null);
            }
            // Tournaments
            if (App.BetContext != null)
            {
                App.BetContext.GetNumberOfTournaments(this.GetNumberOfTournaments_Completed, null);
            }

		}

        /// <summary>
        /// Callback when the number of users was retrieved
        /// </summary>
        /// <param name="operation"></param>
        private void GetNumberOfUsers_Completed(InvokeOperation<int> operation)
        {
            if (!operation.IsCanceled)
            {
                if (!operation.HasError)
                {
                    if (operation.Value > 1)
                    {
                        MembersLabel.Text = String.Format(Helpers.LangHelper.GetString("TotalMembers"), operation.Value);
                    }
                    else
                    {
                        MembersLabel.Text = String.Format(Helpers.LangHelper.GetString("TotalMember"), operation.Value);
                    }
                }
            }
        }

        /// <summary>
        /// Callback when the number of users was retrieved
        /// </summary>
        /// <param name="operation"></param>
        private void GetNumberOfTournaments_Completed(InvokeOperation<int> operation)
        {
            if (!operation.IsCanceled)
            {
                if (!operation.HasError)
                {
                    if (operation.Value > 1)
                    {
                        TournamentsLabel.Text = String.Format(Helpers.LangHelper.GetString("TotalTournaments"), operation.Value);
                    }
                    else
                    {
                        TournamentsLabel.Text = String.Format(Helpers.LangHelper.GetString("TotalTournament"), operation.Value);
                    }
                }
            }
        }

        public void UpdateCulture()
        {
            AboutTitle.Text = Helpers.LangHelper.GetString("TabAbout");
            ComingSoonText.Text = Helpers.LangHelper.GetString("ComingSoonMessage");
        }

	}
}